//发送验证码
function sendMsg(domId,type){
	var obj=$("#"+domId+'Btn');
	if(obj.attr("disabled")=='disabled')return false;
	//手机号
    var phonenum = $("#"+domId+"Phone").val();
    var result = isPhoneNum(phonenum);
	//短信验证码
	var  verify=$("#"+domId+"Code").val();
    if(result){
	   $.post('/public/getverify',{'phone':phonenum,'verify':verify,'type':type},function(r){
			if (r.status) {
				addCookie("secondsremainedwap",r.sTime,r.sTime);//添加cookie记录,有效时间60s
				settime(obj);//开始倒计时
			}else{
                $.toptip(r.info, 'warning')
				$('.sendMsgImg').click();
			}
	   });
    }
}

//开始倒计时
var countdown;
function settime(obj) {
    countdown=getCookieValue("secondsremainedwap");
    if (countdown == 0) {
        obj.removeAttr("disabled").css({'background-color':'transparent','color':'#3cc51f'});
        $("#sendMsgBtn").html("获取短信验证码");
        return;
    } else {
        obj.attr("disabled", true).css({'background':'#9e9b9b','color':'white'});
        $("#sendMsgBtn").html("重新发送(" + countdown + ")");
        countdown--;
        editCookie("secondsremainedwap",countdown,countdown+1);
    }
    setTimeout(function() { settime(obj) },1000) //每1000毫秒执行一次
}
//校验手机号是否合法
function isPhoneNum(phonenum){
    var myreg = /^1[0-9]{10}$/;
    if(!myreg.test(phonenum)){
        $.toptip('请输入有效的手机号码！');
        return false;
    }else{
        return true;
    }
}

//发送验证码时添加cookie
function addCookie(name,value,expiresHours){
    var cookieString=name+"="+escape(value);
    if(expiresHours>0){
        var date=new Date();
        date.setTime(date.getTime()+expiresHours*1000);
        cookieString=cookieString+";expires=" + date.toUTCString();
    }
        document.cookie=cookieString;
}

//修改cookie的值
function editCookie(name,value,expiresHours){
    var cookieString=name+"="+escape(value);
    if(expiresHours>0){
      var date=new Date();
      date.setTime(date.getTime()+expiresHours*1000); //单位是毫秒
      cookieString=cookieString+";expires=" + date.toGMTString();
    }
      document.cookie=cookieString;
}
//根据名字获取cookie的值
function getCookieValue(name){
      var strCookie=document.cookie;
      var arrCookie=strCookie.split("; ");
      for(var i=0;i<arrCookie.length;i++){
        var arr=arrCookie[i].split("=");
        if(arr[0]==name){
          return unescape(arr[1]);
          break;
        }
      }

      return "";
}

//登录
function userlogin(){
    var tag=$("#reg").html();
    if(tag=='验证中..')return false;
    tag='验证中..';
    $("#reg").html(tag);
    $.post($("#form").attr('action'),$("#form").serialize(),function(res){
            if(res.status){
                $.toast(res.info,function(){location.href=res.url;});
                return;
            }
            $("#verifyimg").click();
            $.toptip(res.info,'error');
            $("#reg").html('登  录');
        }
    )
}

//注册提交
function regpost(){
    var obj=$("#reg");
    var tag=obj.html();
    if(tag=='处理中..')return false;
    tag='处理中..';
    obj.html(tag);
    $.post($("#form").attr('action'),$("#form").serialize(),function (res) {
            if(res.status) {
                $.toast(res.info,function(){
                    editCookie("secondsremainedwap",0);
                    location.href=res.url;
                });
                return;
            }
            $.toptip(res.info,'error');
            obj.html('注册并登录');
        }
    )
}

//找回密码
function forgetpost(){
    var tag=$("#reg").html();
    if(tag=='处理中..')return false;
    tag='处理中..';
    $("#reg").html(tag);
    $.post($("#form").attr('action'),$("#form").serialize(),function(res){
            if(res.status){
                $.toast(res.info,function(){editCookie("secondsremainedwap",0);location.href=res.url;});
                return;
            }
            $.toptip(res.info,'error');
            $("#reg").html('修改密码');
        }
    )
}
